//
//  ShareViewController.m
//  SinaRadio1.1.0
//
//  Created by 武 晓方 on 12-1-17.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "BaseSendViewController.h"

#import <QuartzCore/QuartzCore.h>
#import "FXTipView.h"


@interface BaseSendViewController ()

- (void) initTextView;
- (void) initNavigationBar;

@end

@implementation BaseSendViewController
@synthesize textView = _textView;
@synthesize textCountLabel = _textCountLabel;
@synthesize deleteTextButton = _deleteTextButton;
@synthesize shareTextCount;


- (void)dealloc
{
    [_textView release];
    [_textCountLabel release];
    [_deleteTextButton release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    shareTextCount = kShareTextCount;
    [self initTextView];
    [self initNavigationBar];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_texture.png"]];

    
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return NO;
}

- (void) initTextView
{
    
    UIImageView* bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
//    [bgImageView setImage:[UIImage imageNamed:@"background.png"]];
    
    [self.view addSubview:bgImageView];
    [bgImageView release];
    bgImageView = nil;
    
    
  
    
    CGRect backgroundViewRect = CGRectMake(kTextViewOriange_x, kTextViewOriange_y, self.view.frame.size.width - 2*kTextViewOriange_x, kTextViewHeigth);
    
   
    
    UIView* leftTextBackgroundView = [[UIView alloc] initWithFrame:backgroundViewRect];
    leftTextBackgroundView.backgroundColor = [UIColor clearColor];
    leftTextBackgroundView.backgroundColor = [UIColor colorWithRed:227.0/255.0 green:227.0/255.0 blue:227.0/255.0 alpha:1.0];
    leftTextBackgroundView.layer.cornerRadius = 5.0f;
    leftTextBackgroundView.layer.borderWidth = 1.0f;
    leftTextBackgroundView.layer.borderColor = [UIColor colorWithRed:255.0/255.0 green:153.0/255.0 blue:52.0/255.0 alpha:1.0].CGColor;
    [self.view addSubview:leftTextBackgroundView];
    

    _textView = [[UITextView  alloc] initWithFrame:CGRectMake(10, 10, backgroundViewRect.size.width - 20, backgroundViewRect.size.height - kDeleteButton - 20)] ; //初始化大小并自动释放
    _textView.font = [UIFont fontWithName:@"Arial" size:14.0]; 
    _textView.delegate = self; 
    _textView.backgroundColor = [UIColor clearColor];
    [_textView becomeFirstResponder];
    [_textView setFont:[UIFont systemFontOfSize:16]];
    [leftTextBackgroundView addSubview:_textView];
    

    _textCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(backgroundViewRect.size.width - 2*kDeleteButton - 20 - 10, backgroundViewRect.size.height - kDeleteButton - 10, kDeleteButton + 10, kDeleteButton)];
    [_textCountLabel setBackgroundColor:[UIColor clearColor]];
    [_textCountLabel setTextAlignment:UITextAlignmentCenter];
    [_textCountLabel setFont:[UIFont systemFontOfSize:12]];
    [leftTextBackgroundView addSubview:_textCountLabel];
    
    
    _deleteTextButton = [[UIButton alloc] initWithFrame:CGRectMake(backgroundViewRect.size.width - kDeleteButton - 5 - 10, backgroundViewRect.size.height - kDeleteButton - 10, kDeleteButton , kDeleteButton)];
    [_deleteTextButton setImage:[UIImage imageNamed:@"delete_text.png"] forState:UIControlStateNormal];
    [_deleteTextButton setImage:[UIImage imageNamed:@"delete_text_touch.png"] forState:UIControlStateHighlighted];
    [_deleteTextButton addTarget:self action:@selector(deleteTextButtonPress) forControlEvents:UIControlEventTouchUpInside];
    [leftTextBackgroundView addSubview:_deleteTextButton];
    
    [leftTextBackgroundView release];
    

    

}

- (void) initNavigationBar
{

}

- (void) deleteTextButtonPress
{
    _textView.text = nil;
    [_textCountLabel setText:[NSString stringWithFormat:@"%d",shareTextCount]];
}
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{    
    NSInteger remainCount = [_textView.text length];
    
    if (remainCount >= shareTextCount) 
    {
        NSString * limitedMsg = [_textView.text substringToIndex:shareTextCount];
        [FXTipView showTipViewWithText:[NSString stringWithFormat:@"最多可输入%d个字符",shareTextCount]];
        _textView.text = limitedMsg;
        [_textCountLabel setText:[NSString stringWithFormat:@"%d",0]];
    }
    else
    {
        [_textCountLabel setText:[NSString stringWithFormat:@"%d",shareTextCount - remainCount]];
    }
}
@end
